package com.huluobo.koltinim2003a.extention

import androidx.fragment.app.Fragment
import com.huluobo.koltinim2003a.R
import com.huluobo.koltinim2003a.ui.fragment.ContactFragment
import com.huluobo.koltinim2003a.ui.fragment.ConversationFragment
import com.huluobo.koltinim2003a.ui.fragment.DynamicFragment

/**
 * Created by lc on 2022/9/23.
 * Fragment工厂+单例模式
 * 1.私有化构造函数,单例模式的核心
 * 2.懒汉饿汉模式,加同步锁
 * 3.private constructor()是kotlin中私有构造函数的写法
 */
class FragmentFactory private constructor() {
    //对外暴露工厂的实例,为了外部调用到
    companion object {
        val instance = FragmentFactory()
    }

    private val conversation by lazy {
        ConversationFragment()
    }

    private val contact by lazy {
        ContactFragment()
    }

    private val dynamic by lazy {
        DynamicFragment()
    }

    fun setFragment(tabId: Int): Fragment? {
        when (tabId) {
            R.id.tab_conversation -> return conversation
            R.id.tab_contacts -> return contact
            R.id.tab_dynamic -> return dynamic
        }
        return null
    }

}